home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
COLLECT.H
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
3KB
|
78 lines
//--- COLLECT.H ------------------------ Listing 1 -----------
// A record and its container class.
// Code reuse & modifying existing classes with inheritance.
//
// (c) 1991 C Gazette. Object code may be used freely. Source
// code may be used as long as authorship and publication are
// explicitly acknowledged.
//------------------------------------------------------------
#ifndef _COLLECT_H_
#define _COLLECT_H_
#include <stdio.h>
#include <string.h>
#include <array.h> // Object and Array class declarations
const NameSize = 40,
DescriptionSize = 512;
// Objects can be held in Arrays. With inheritance,
// so can records:
class record : public Object {
char name[NameSize], description[DescriptionSize];
public:
record ( char * Name, char * Description ) {
strncpy ( name, Name, NameSize );
// force null termination (see strncpy)
name[NameSize - 1] = '\0';
strncpy ( description, Description, DescriptionSize );
description[DescriptionSize - 1] = '\0';
}
char * Name() { return name; }
char * Description() { return description; }
// dummy definitions for pure virtual functions:
virtual classType isA() const { return 0; }
char * nameOf() const { return "record"; }
hashValueType hashValue() const { return 0; }
int isEqual(const Object&) const { return 1; }
void printOn(ostream&) const { }
};
// Making a special type of Array to hold records:
class recordArray : public Array {
int size; // change the way it keeps track of size
public:
recordArray() : Array ( 0, 0, 1 ) { size = 0; }
// Build a new one from an existing one,
// removing the null spots:
// The copy-constructor
recordArray(recordArray& rv) : Array ( 0, 0, 1 ) {
size = 0;
for ( int i = 0; i < rv.arraySize(); i++ )
if ( !rv.null( i ))
add ( new record ( rv.rec ( i )));
// ( Note: record(record&) is automatically
// generated by compiler! )
}
// specifying the insert and fetch:
void add ( record* r ) { Array::add ( *r ); size++; }
// use this instead of operator[]:
record& rec ( int i )
{ return (record&)Array::operator[] ( i ); }
int arraySize() { return size; } // redefinition
void destroy ( int i ) {
Array::destroy ( i );
theArray[i] = 0;
}
// is this slot empty?
int null ( int i ) { return ! ( theArray[i] ); }
};
#endif // _COLLECT_H_